package com.example.alternateThread.syn;

public class AlternateThread1 extends Thread {
    static int flag = 0;
    int loop = 0;
    int num = 0;
    char mark = 0;

    public AlternateThread1(int num, char mark) {
        this.num = num;
        this.mark = mark;
    }

    @Override
    public void run() {
        while (loop < 5) {
            synchronized (AlternateThread1.class) {
                while (num != flag) {
                    try {
                        AlternateThread1.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(mark);
                flag = (flag + 1) % 3;
                AlternateThread1.class.notifyAll();
                loop++;
            }
        }
    }

    public static void main(String[] args) {
        new AlternateThread1(0, 'A').start();
        new AlternateThread1(1, 'B').start();
        new AlternateThread1(2, 'C').start();
    }
}
